<?php

/**
 * categorie actions.
 *
 * @package    gesp
 * @subpackage categorie
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class categorieActions extends sfActions {
	public function executeIndex(sfWebRequest $request) {
		$prj_id = $this->getUser()->getAttribute('prj_id');
		$q = Doctrine_Query::create()
			->from('Categorie')
			->where('cat_prj_id = ?', $prj_id);
		$this->categories = $q->execute();
	}

	public function executeShow(sfWebRequest $request) {
		$this->forward404Unless($this->categorie = Doctrine_Core::getTable('Categorie')->find(array($request->getParameter('cat_id'))), sprintf('Object categorie does not exist (%s).', $request->getParameter('cat_id')));
	}
	
	public function executeNew(sfWebRequest $request) {
		$prj_id = $this->getUser()->getAttribute('prj_id');
		$categorie = new Categorie();
		$categorie->setCatPrjId($prj_id);
		$this->form = new CategorieForm($categorie);
	}

	public function executeCreate(sfWebRequest $request) {
		$this->forward404Unless($request->isMethod(sfRequest::POST));

		$this->form = new CategorieForm();

		$this->processForm($request, $this->form);

		$this->setTemplate('new');
	}

	public function executeEdit(sfWebRequest $request) {
		$this->forward404Unless($categorie = Doctrine_Core::getTable('Categorie')->find(array($request->getParameter('cat_id'))), sprintf('Object categorie does not exist (%s).', $request->getParameter('cat_id')));
		$this->form = new CategorieForm($categorie);
	}

	public function executeUpdate(sfWebRequest $request) {
		$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
		$this->forward404Unless($categorie = Doctrine_Core::getTable('Categorie')->find(array($request->getParameter('cat_id'))), sprintf('Object categorie does not exist (%s).', $request->getParameter('cat_id')));
		$this->form = new CategorieForm($categorie);

		$this->processForm($request, $this->form);

		$this->setTemplate('edit');
	}

	public function executeDelete(sfWebRequest $request) {
		$request->checkCSRFProtection();

		$this->forward404Unless($categorie = Doctrine_Core::getTable('Categorie')->find(array($request->getParameter('cat_id'))), sprintf('Object categorie does not exist (%s).', $request->getParameter('cat_id')));
		$categorie->delete();

		$this->redirect('categorie/index');
	}

	protected function processForm(sfWebRequest $request, sfForm $form) {
		$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
		if ($form->isValid()) {
			$categorie = $form->save();

			$this->redirect('categorie/index');
		}
	}
}
